package com.cart.protocol;

import com.cart.utils.FormatUtils;
import com.cart.utils.ProtocolUtils;

/*
    ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ 
       Author   :  lixiaodaoaaa
       Date     :  2020-01-15
       Time     :  11:23
    ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 */
public class CommandCreateFactory extends CommandFactory {

    public static String buildReadModuleStatus(String moduleName) {
        String commandWord = ProtocolDefine.CommandWordSend.READ;
        String deviceName = moduleName;
        String operator = ProtocolDefine.OperateCommand.READ;
        String content = CreateContentFactory.createContent(commandWord, deviceName, operator);
        return buildCommand(content);
    }

    /*
    44332211,
    表示首次去皮的AD采样值0x11223344，
    由上位机保存。
    如果去皮命令的指令内容为
    00
    00
    00
    00，
    则由下位机自动去皮，
    并向上位机发送去皮时的AD采样值,
    上位机保存
     */
    public static String buildSetModule(String moduleName, String innerContent) {
        String commandWord = ProtocolDefine.CommandWordSend.ACTION;
        String deviceName = moduleName;
        String operator = ProtocolDefine.OperateCommand.SET;

        String content = CreateContentFactory.createContent(commandWord, deviceName, operator, innerContent);
        return buildCommand(content);
    }


    public static String buildSetModule(String moduleName, float value) {
        String commandWord = ProtocolDefine.CommandWordSend.ACTION;
        String deviceName = moduleName;
        String operator = ProtocolDefine.OperateCommand.SET;

        int oxygenIntValue = Math.round(value * 100);
        String hexValue = FormatUtils.intValueTo4HexString(oxygenIntValue);
        String setValueContent = FormatUtils.revertString(hexValue);
        String content = CreateContentFactory.createContent(commandWord, deviceName, operator, setValueContent);
        return buildCommand(content);
    }


    //emergencyStop
    public static String buildEmergencyStopDoorCommand() {
        String commandWord = ProtocolDefine.CommandWordSend.ACTION;
        String deviceName = ProtocolDefine.ModuleName.DOOR;
        String operator = ProtocolDefine.OperateCommand.STOP;
        String content = CreateContentFactory.createContent(commandWord, deviceName, operator);
        return buildCommand(content);
    }

    public static String buildStopModuleCommand(String moduleName) {
        String commandWord = ProtocolDefine.CommandWordSend.ACTION;
        String deviceName = moduleName;
        String operator = ProtocolDefine.OperateCommand.CLOSE;
        String content = CreateContentFactory.createContent(commandWord, deviceName, operator);
        return buildCommand(content);
    }

    public static String buildOpenModuleCommand(String moduleName) {
        String commandWord = ProtocolDefine.CommandWordSend.ACTION;
        String deviceName = moduleName;
        String operator = ProtocolDefine.OperateCommand.OPEN;
        String content = CreateContentFactory.createContent(commandWord, deviceName, operator);
        return buildCommand(content);
    }

    public static String buildOpenModuleCommand(String moduleName, String innerContent) {
        String commandWord = ProtocolDefine.CommandWordSend.ACTION;
        String deviceName = moduleName;
        String operator = ProtocolDefine.OperateCommand.OPEN;
        String content = CreateContentFactory.createContent(commandWord, deviceName, operator, innerContent);
        return buildCommand(content);
    }


    public static String buildCloseModuleCommand(String moduleName) {
        String commandWord = ProtocolDefine.CommandWordSend.ACTION;
        String deviceName = moduleName;
        String operator = ProtocolDefine.OperateCommand.CLOSE;
        String content = CreateContentFactory.createContent(commandWord, deviceName, operator);
        return buildCommand(content);
    }


    public static String buildPowerOffCommand() {
        String commandWord = ProtocolDefine.CommandWordSend.ACTION;
        String deviceName = ProtocolDefine.ModuleName.SYSTEM_POWER;
        String operator = ProtocolDefine.OperateCommand.POWER_OFF;

        String content = CreateContentFactory.createContent(commandWord, deviceName, operator);
        return buildCommand(content);
    }

    //将构建的协议增加 头部尾部 校验和共同组建共同的协议内容
    private static String buildCommand(String protocolContent) {
        String addCheck = ProtocolUtils.getAddCheckResult(protocolContent);
        return new StringBuilder().append(ProtocolDefine.HEADER).append(protocolContent).append(addCheck)
                                  .append(ProtocolDefine.END).toString();
    }
}
